<?php namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;

class AuthController extends Controller
{

  /*
  |--------------------------------------------------------------------------
  | Registration & Login Controller
  |--------------------------------------------------------------------------
  |
  | This controller handles the registration of new users, as well as the
  | authentication of existing users. By default, this controller uses
  | a simple trait to add these behaviors. Why don't you explore it?
  |
  */

  use AuthenticatesAndRegistersUsers;

  /**
   * Create a new authentication controller instance.
   *
   * @param  \Illuminate\Contracts\Auth\Guard $auth
   * @param  \Illuminate\Contracts\Auth\Registrar $registrar
   */
  public function __construct(Guard $auth, Registrar $registrar)
  {
    $this->auth = $auth;
    $this->registrar = $registrar;

    $this->middleware('guest', ['except' => 'getLogout']);
  }

  /**
   * 登录验证.
   *
   * @param  \Illuminate\Http\Request $request
   * @return \Illuminate\Http\Response
   */
  public function postLogin(Request $request)
  {
    $rules = [
        'name' => 'required',
        'password' => 'required',
    ];

    $this->validate($request, $rules);

    $credentials = $request->only('name', 'password');

    if ($this->auth->attempt($credentials, $request->has('remember'))) {
      return redirect()->intended($this->redirectPath());
    }

    return redirect($this->loginPath())
        ->withInput($request->only('name', 'remember'))
        ->withErrors([
            'error' => $this->getFailedLoginMessage(),
        ]);
  }

  /**
   * 登录失败信息
   *
   * @return string
   */
  protected function getFailedLoginMessage()
  {
    return '登录失败，账号不存在或者密码错误.';
  }

}
